package com.cy.config;

import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.GatewayCallbackManager;
import com.alibaba.fastjson.JSON;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

import java.util.HashMap;

/**
 * @author tang
 * @data 2021/9/23
 * @apiNode
 */
@Configuration
public class GatewayConfig {
    public GatewayConfig() {
        GatewayCallbackManager.setBlockHandler(
                (serverWebExchange, throwable) -> {
                    HashMap<String, Object> map = new HashMap<>();
                    map.put("status", 429);
                    map.put("message", "too many request");
                    String jsonstr = JSON.toJSONString(map);
                    return ServerResponse.ok().body(Mono.just(jsonstr), String.class);
                }
        );
    }
}
